package com.tobeing.calendar.lib;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * Created by ToBeing on 2017/10/13/013.
 */

public class CalendarAdapter extends ArrayAdapter<ToBeingDate> {
    private LayoutInflater mInflater;
    private Calendar mCalendar;

    public CalendarAdapter(Context context, Calendar calendar, List<ToBeingDate> listDate) {
        super(context, R.layout.calendar_adapter, listDate);
        mInflater = LayoutInflater.from(context);
        mCalendar = calendar;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ToBeingDate date = getItem(position);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.calendar_adapter, parent, false);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        ((CalendarDay)convertView).setText(sdf.format(date.getDate()));

        boolean isTheSameMonth = false;
        if (date.getDate().getMonth() == mCalendar.getTime().getMonth()) {
            isTheSameMonth = true;
        }
        if (isTheSameMonth) {
            ((TextView) convertView).setTextColor(Color.BLACK);
        } else {
            ((TextView) convertView).setTextColor(Color.GRAY);
        }

        if(date.isSelected()){
            ((CalendarDay)convertView).setSelected(true);
        }
        return convertView;
    }
}
